<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24
 * Time: 13:56
 */

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use think\app;
use think\facade\Db;
use think\facade\View;


class Account extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\mall\Account');
        $this->storemodel = app('app\common\model\mall\Store');
    }
        public function get_where($array= []){

            $arr[] = ['type','=',2];

            $id = $this->storemodel->column('id');
            if(!empty($id)){
                $arr[] = ['store_id','in',$id];
            }
            if(isset($array['category_id']) && !empty($array['category_id'])){
                $arr[] = ['category_id','=',$array['category_id']];
            }
            if(isset($array['title']) && !empty($array['title'])){
                $arr[] = ['title','like','%'.$array['title'].'%'];
            }
            return $arr;
        }

    public function otherData(){
        $model = app('app\common\model\common\Categories');

        $data = $model->field('id,name,parent_id')->with(['childs'=>function($query){
            $query->where(['type'=>2])->with(['childs'=>function($query){
                $query->where(['type'=>2])->select();
            }]);
        }
        ])->where(['parent_id'=>0,'type'=>2])->order('sort asc')->select();

        View::assign('data',$data);
    }

    public function get_other(){
        $model = app('app\common\model\common\Categories');

        $data = $model->field('id,name,parent_id')->with(['childs'=>function($query){
            $query->where(['type'=>2])->with(['childs'=>function($query){
                $query->where(['type'=>2])->select();
            }]);
        }
        ])->where(['parent_id'=>0,'type'=>2])->order('sort asc')->select();

        View::assign('data',$data);
    }
    public function get_with(){
        return ['store','category'];
    }

    public function action(){

        $param= $this->request->param();

        try{
            $row = $this->model->where('id',$param['id'])->find();
            $row->save($param);

        }catch (\Exception $exception){
            return json(['code'=>1,'msg'=>$exception->getMessage()]);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }

    /**
     * 导出课程
     */
    public function export(){
        $data = $this->model->with(['store','category'])->order('sale desc')->select()->toArray();
        $dataArray = [];
        foreach($data as $k=>$v){
            if($v['store']['name'] != '' && $v['store_id'] !=56){
                $dataArray[$k]['id'] = $v['id'];

                $dataArray[$k]['store'] = $v['store']['name'];
                $dataArray[$k]['title'] = $v['title'];
                $dataArray[$k]['price'] = $v['price'];
                $dataArray[$k]['cate'] = $v['category']['name'];
                $dataArray[$k]['sale'] = $v['sale'];
                $dataArray[$k]['amount'] = $v['price'] * $v['sale'];
            }


        }

        $tableheader = ['id','机构','课程名','单价','分类','销量','销售额'];
        excelFileExport($dataArray,$title='线下课程数据',$tableheader);
    }
}